package com.etd.base.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Map;

import com.etd.base.config.ConstSysParam;

public class DateUtil {

	private DateUtil(){};	
	
	private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.SIMPLIFIED_CHINESE);
	public static synchronized String date2JsonStr(Date date){
		
		String retDate = null;
		
		if(date!=null){
			try{
				retDate = sdf.format(date);
			}catch(Exception e){
				e.printStackTrace();				
			}
		}
		return retDate;
	}
	
	public static synchronized Date jsonStr2Date(String _date){
		
		
		if(_date ==null || _date.trim().compareTo("")==0){
			return null;
		}else{
			_date = _date.trim();
		}
		
		Date retDate = null;
		String _tmpDate = null;
		if(_date!=null){
			Integer _dateLength = _date.length();
			if(_dateLength==19){
				_tmpDate = _date;
			}else{
				if(_dateLength>19)
					_tmpDate = _date.substring(0,19);
				else{
					switch(_dateLength){
						case 10
							:_tmpDate =_date + " 00:00:00";
							break;
						case 13
							:_tmpDate= _date + ":00:00";
							break;
						case 16
							:_tmpDate =  _date = ":00";
							break;
						default
							:break;
					}
				}
			}
		}
		
		
		try{
			retDate = sdf.parse(_tmpDate);
		}catch(Exception e){
			e.printStackTrace();
		}
		return retDate;
	}
	
	/**
	 * 获取昨日所在月的首日和末日
	 * @return
	 */
	public static String[] getYestodayMonthDay(){
			String[] retVal = new String[3];
			Calendar c = Calendar.getInstance();
			
			c.add(Calendar.DATE, -1);
			int year = c.get(Calendar.YEAR);
			int month = c.get(Calendar.MONTH)+1;
			int days = c.getActualMaximum(Calendar.DAY_OF_MONTH);
			String start = String.format("%1$d-%2$d-%3$d", year,month,1);
			String end = String.format("%1$d-%2$d-%3$d", year,month,days);
			retVal[0] = start;
			retVal[1] = end;
		    retVal[2] = year + "-"+month;
			return retVal;
	}
	
	/**
	 * 获取指定日期之前的特定日期
	 * @param endDate
	 * @param before
	 * @return
	 */
	public static String getDateBefore(String endDate,int before){
		Date date = null;
		try {
			date = (new SimpleDateFormat("yyyy-MM-dd")).parse(endDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(date !=null){
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			cal.add(Calendar.DATE, 0-before);
			return (new SimpleDateFormat("yyyy-MM-dd")).format(cal.getTime());
		}else{
			return null;
		}
	}
	
	/**
	 * 获取指定年月的首日和末日
	 * @return
	 */
	public static String[] getMonthDay(Integer year,Integer month){
			String[] retVal = new String[3];
			Calendar c = Calendar.getInstance();
			
			c.set(Calendar.YEAR, year);
			c.set(Calendar.MONTH, month-1);
			c.set(Calendar.DATE, 1);
			
			int days = c.getActualMaximum(Calendar.DAY_OF_MONTH);
			String start = String.format("%1$d-%2$d-%3$d", year,month,1);
			String end = String.format("%1$d-%2$d-%3$d", year,month,days);
			retVal[0] = start;
			retVal[1] = end;
		    retVal[2] = year + "-"+month;
			return retVal;
	}
	
	/**
	 * 获取参数map中的年月起始日期和结束日期
	 * @param paramMap
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static String[] getMapMonthDay(Map<String,Object> paramMap){
		String[] retVal = new String[3];
		if(paramMap!=null &&paramMap.containsKey(ConstSysParam.CONDITION_MAP_KEY)){
			Map<String,String> conditionMap = (Map<String, String>) paramMap.get(ConstSysParam.CONDITION_MAP_KEY);
			
			Integer year = null;
			Integer month = null;
			if(conditionMap.containsKey("monitor_year_month")){
				String yearMonth = conditionMap.get("monitor_year_month");
				if(yearMonth!=null && yearMonth.trim().length()>0){
					String[] ym = yearMonth.split("-");
					if(ym!=null && ym.length>1){
						year = LongUtil.getIntValue(ym[0],null);
						month = LongUtil.getIntValue(ym[1],null);
					}
				}
			}else{
				if(conditionMap.containsKey("monitor_moth") && conditionMap.containsKey("monitor_year")){			
					month = LongUtil.getIntValue(conditionMap.get("monitor_moth"),null);
					year = LongUtil.getIntValue(conditionMap.get("monitor_year"),null);
				}
				
			}
			
			if(year==null){
				if(conditionMap.containsKey("evaluate_time_end")){
					String yearMonth = conditionMap.get("evaluate_time_end");
					if(yearMonth!=null && yearMonth.trim().length()>0){
						String[] ym = yearMonth.split("-");
						if(ym!=null && ym.length>1){
							year = LongUtil.getIntValue(ym[0],null);
							month = LongUtil.getIntValue(ym[1],null);
						}
					}
				}
			}
			
			if(year!=null && month!=null){
				Calendar c = Calendar.getInstance();
				c.set(Calendar.YEAR,year);
				c.set(Calendar.MONTH,month-1);
				
				int days = c.getActualMaximum(Calendar.DAY_OF_MONTH);
				String start = String.format("%1$d-%2$d-%3$d", year,month,1);
				String end = String.format("%1$d-%2$d-%3$d", year,month,days);
				retVal[0] = start;
				retVal[1] = end;
				retVal[2]=year+"-"+month;
			}
		}
		return retVal;
	}
	
	public static Date addDate(Date date,int i){
		if(date == null){
			date = new Date();
		}
		
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.DATE,c.get(Calendar.DATE)+i);
		return c.getTime();
	}
	
	/**
	 * 获取月度最后一天
	 * @param date
	 * @return
	 */
	public static Date getLastDayofMonth(Date date){
		if(date !=null){
			Calendar c = Calendar.getInstance();
			c.setTime(date);
			int d = c.getActualMaximum(Calendar.DAY_OF_MONTH);
			c.set(Calendar.DAY_OF_MONTH, d);
			return c.getTime();
		}
		return null;
	}
	
	
}
